<template>
  <van-datetime-picker
    v-model="currentDate"
    type="date"
    title="选择生日"
    :min-date="minDate"
    :max-date="maxDate"
    @cancel="$emit('close')"
    @confirm="onConfirm"
  />
</template>

<script>
import { setUserMessage } from '@/api/user'
import dayjs from 'dayjs'
export default {
  name: 'UpdateBirthday',
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value)
    }
  },
  methods: {
    async onConfirm (value) {
      this.$toast.loading({
        message: '保存中...',
        forbidClick: true, // 禁止背景点击
        duration: 0 // 持续展示
      })
      try {
        await setUserMessage({
          birthday: dayjs(value).format('YYYY-MM-DD')
        })
        this.$toast.success('更新成功')

        this.$emit('input', dayjs(value).format('YYYY-MM-DD'))
        this.$emit('close')
      } catch (err) {
        console.log(err)
        this.$toast.fail('更新失败')
      }
    }
  }
}
</script>

<style>
</style>
